﻿namespace KeRui.TemplateMaker
{
    using KeRui.BLL;
    using System;
    using System.Data;
    using System.Text;

    public class TagData
    {
        private static DataTable _DataForParameter(string parameter)
        {
            string str6;
            parameter = parameter.Replace("parameter_", string.Empty);
            Common common = new Common();
            TagModule module = new TagModule();
            DataRow row = module.GetModelByName(parameter).Rows[0];
            string str = row["DataTable"].ToString();
            string str2 = (row["TopNum"].ToString() == "0") ? "*" : row["TopNum"].ToString();
            string str3 = row["StrWhere"].ToString();
            string str4 = row["OrderBy"].ToString();
            string str5 = row["Direction"].ToString();
            if (str2 == "*")
            {
                if (string.IsNullOrEmpty(str3))
                {
                    str6 = string.Format("SELECT  ROW_NUMBER() OVER(ORDER BY {1} DESC) AS rowscount ,* FROM {0} ORDER BY {1} {2}", str, str4, str5);
                }
                else
                {
                    str6 = string.Format("SELECT   ROW_NUMBER() OVER(ORDER BY {1} DESC) AS rowscount ,* FROM {0} WHERE {3} ORDER BY {1} {2}", new object[] { str, str4, str5, str3 });
                }
            }
            else if (string.IsNullOrEmpty(str3))
            {
                str6 = string.Format("SELECT TOP({3})   ROW_NUMBER() OVER(ORDER BY {1} DESC) AS rowscount ,* FROM {0} ORDER BY {1} {2}", new object[] { str, str4, str5, str2 });
            }
            else
            {
                str6 = string.Format("SELECT TOP({4})   ROW_NUMBER() OVER(ORDER BY {1} DESC) AS rowscount ,* FROM {0} WHERE {3} ORDER BY {1} {2}", new object[] { str, str4, str5, str3, str2 });
            }
            DataTable table = new DataTable();
            if (common.GetDataTableBySql(str6).Tables.Count > 0)
            {
                table = common.GetDataTableBySql(str6).Tables[0];
            }
            return table;
        }

        public static DataTable DataForNodeDetails(int nodeID, int top, string strWhere, string range, string orderby, bool includeChild)
        {
            return DataForNodeID(nodeID, top, strWhere, range, orderby, includeChild);
        }

        public static DataTable DataForNodeDetails2(int nodeID, int top, string strWhere, string range, string orderby, bool includeChild)
        {
            return DataForNodeID2(nodeID, top, strWhere, range, orderby, includeChild);
        }

        public DataTable DataForNodeID(NodeAttribute resolveAttribute)
        {
            string str;
            StringBuilder builder = new StringBuilder();
            if (resolveAttribute.IsHot)
            {
                builder.Append(" AND IsHot='TRUE'");
            }
            if (resolveAttribute.IsMarked)
            {
                builder.Append(" AND IsMarked='TRUE'");
            }
            if (resolveAttribute.IsRecommend)
            {
                builder.Append(" AND IsRecommend='TRUE'");
            }
            if (resolveAttribute.IsTop)
            {
                builder.Append(" AND IsTop='TRUE'");
            }
            if (!(string.IsNullOrEmpty(resolveAttribute.ContentType.ToString()) || (resolveAttribute.ContentType == 0)))
            {
                builder.Append(" AND ContentType=" + resolveAttribute.ContentType + " ");
            }
            if (string.IsNullOrEmpty(resolveAttribute.OrderBy))
            {
                str = " ORDER BY cc.PublishTime DESC";
            }
            else if (resolveAttribute.OrderBy.ToLower() == "contentid")
            {
                str = string.Format(" ORDER BY cc.{0} {1}", resolveAttribute.OrderBy, resolveAttribute.Direction);
            }
            else
            {
                str = string.Format(" ORDER BY {0} {1}", resolveAttribute.OrderBy, resolveAttribute.Direction);
            }
            if (resolveAttribute.NodeID == 0)
            {
                return null;
            }
            DataTable table = new DataTable();
            return DataForNodeID(resolveAttribute.NodeID, 100, builder.ToString(), "", str, resolveAttribute.IsIncludeChild);
        }

        private static DataTable DataForNodeID(int nodeID, int top, string strWhere, string range, string orderby, bool includeChild)
        {
            string str;
            if (top == 0)
            {
                str = "";
            }
            else
            {
                str = string.Format("TOP {0}", top);
            }
            CmsContent content = new CmsContent();
            if (!includeChild)
            {
                return content.GetCmsContentDetailsByNodeID(nodeID, str, strWhere, range, (orderby == string.Empty) ? "ORDER BY cc.PublishTime DESC" : orderby);
            }
            return content.GetCmsContentDetailsByParentNodeID(nodeID, str, strWhere, range, (orderby == string.Empty) ? "ORDER BY cc.PublishTime DESC" : orderby);
        }

        private static DataTable DataForNodeID2(int nodeID, int top, string strWhere, string range, string orderby, bool includeChild)
        {
            CmsContent content = new CmsContent();
            if (!includeChild)
            {
                return content.GetCmsContentDetailsByNodeID(nodeID, "", strWhere, range, (orderby == string.Empty) ? "ORDER BY cc.PublishTime DESC" : orderby);
            }
            return content.GetCmsContentDetailsByParentNodeID(nodeID, "", strWhere, range, (orderby == string.Empty) ? "ORDER BY cc.PublishTime DESC" : orderby);
        }

        public DataTable DataForParameter(string parameter)
        {
            return _DataForParameter(parameter);
        }

        public DataTable DataForVocationID(SellerVocationAttribute resolveAttribute)
        {
            Seller seller = new Seller();
            DataTable table = new DataTable();
            return seller.GetDataForTag(resolveAttribute.VocationID);
        }
    }
}

